package com.deductive.web.disposition.layer;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.deductive.domain.layer.Layer;
import com.deductive.mapper.layer.LayerMapper;
import com.deductive.common.core.controller.BaseController;
import com.deductive.common.core.domain.AjaxResult;
import com.deductive.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/layer")
@Api(tags = "地图服务管理")
public class LayerController extends BaseController {

    @Autowired
    private LayerMapper layerMapper;

    @ApiOperation("获取图层")
    @GetMapping("/getLayerList")
    public AjaxResult getLayerList(Layer layer) {
        List<Layer> layerList = layerMapper.getLayerList(layer);
        return AjaxResult.success(layerList);
    }

    @ApiOperation("获取图层分页")
    @GetMapping("/getLayerListLimit")
    public TableDataInfo getLayerListLimit(Layer layer) {
        startPage();
        List<Layer> layerList = layerMapper.getLayerList(layer);
        return getDataTable(layerList);
    }

    @ApiOperation("根据Id获取图层详情")
    @GetMapping("/getLayerById/{id}")
    public AjaxResult getLayerById(@PathVariable String id) {
        Layer layerById = layerMapper.getLayerById(id);
        return AjaxResult.success(layerById);
    }

    @ApiOperation("根据Id更新图层")
    @PutMapping("/updateLayer")
    public AjaxResult updateLayer(@RequestBody Layer layer) {
        layerMapper.updateLayer(layer);
        return AjaxResult.success();
    }

    @ApiOperation("添加图层")
    @PostMapping("/insertLayer")
    public AjaxResult insertLayer(@RequestBody Layer layer) {
        layer.setId(IdWorker.getIdStr());
        layerMapper.insertLayer(layer);
        return AjaxResult.success();
    }

    @ApiOperation("删除图层")
    @DeleteMapping("/deleteLayer")
    public AjaxResult deleteLayer(@RequestParam("idList") List<String> idList) {
        for (String id : idList) {
            layerMapper.deleteLayer(layerMapper.getLayerById(id));
        }
        return AjaxResult.success();
    }
}